load("//def:configmap.bzl", "k8s_configmap")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@io_bazel_rules_k8s//k8s:object.bzl", "k8s_object")
load("@io_bazel_rules_k8s//k8s:objects.bzl", "k8s_objects")
load("//prow:def.bzl", "prow_image")

go_library(
    name = "go_default_library",
    srcs = ["main.go"],
    importpath = "k8s.io/test-infra/prow/cmd/peribolos",
    visibility = ["//visibility:private"],
    deps = [
        "//prow/config/org:go_default_library",
        "//prow/config/secret:go_default_library",
        "//prow/flagutil:go_default_library",
        "//prow/github:go_default_library",
        "//prow/logrusutil:go_default_library",
        "@com_github_sirupsen_logrus//:go_default_library",
        "@io_k8s_apimachinery//pkg/util/errors:go_default_library",
        "@io_k8s_apimachinery//pkg/util/sets:go_default_library",
        "@io_k8s_sigs_yaml//:go_default_library",
    ],
)

NAME = "peribolos"

go_binary(
    name = NAME,
    embed = [":go_default_library"],
    pure = "on",
    visibility = ["//visibility:public"],
)

filegroup(
    name = "package-srcs",
    srcs = glob(["**"]),
    tags = ["automanaged"],
    visibility = ["//visibility:private"],
)

filegroup(
    name = "all-srcs",
    srcs = [":package-srcs"],
    tags = ["automanaged"],
    visibility = ["//visibility:public"],
)

go_test(
    name = "go_default_test",
    srcs = ["main_test.go"],
    embed = [":go_default_library"],
    deps = [
        "//prow/config/org:go_default_library",
        "//prow/flagutil:go_default_library",
        "//prow/github:go_default_library",
        "@com_github_google_go_cmp//cmp:go_default_library",
        "@io_k8s_apimachinery//pkg/util/diff:go_default_library",
        "@io_k8s_apimachinery//pkg/util/sets:go_default_library",
        "@io_k8s_sigs_yaml//:go_default_library",
    ],
)

# Usage:
# bazel run :e2e-dump -- --github-token-path ~/whatever # --dump=whatever etc
go_binary(
    name = "e2e",
    args = [
        "--tokens=0",
    ],
    data = [":test-config.yaml"],
    embed = [":go_default_library"],
)

go_binary(
    name = "e2e-dump",
    args = [
        "--tokens=0",
        "--dump=fejtaverse",
    ],
    embed = [":go_default_library"],
)

go_binary(
    name = "e2e-change",
    args = [
        "--tokens=0",
        "--config-path=prow/cmd/peribolos/test-config.yaml",
        "--min-admins=2",
        "--fix-org",
        "--fix-org-members",
        "--fix-teams",
        "--fix-team-members",
    ],
    data = [":test-config.yaml"],
    embed = [":go_default_library"],
)

# Usage:
#   bazel run :dev-job.{create,delete,describe}
CLUSTER = "gke_fejta-prod_us-central1-f_erick"  # TODO(fejta): fix

k8s_object(
    name = "peribolos-job",
    cluster = CLUSTER,
    image_chroot = "{STABLE_DOCKER_REPO}",
    images = {"gcr.io/k8s-testimages/peribolos:latest": ":image"},
    kind = "Job",
    template = ":dev.yaml",
)

k8s_configmap(
    name = "peribolos-test-config",
    cluster = CLUSTER,
    data = {
        "config.yaml": ":test-config.yaml",
    },
)

k8s_objects(
    name = "dev",
    objects = [
        ":peribolos-job",
        ":peribolos-test-config",
    ],
)

prow_image(
    name = "image",
    base = "@alpine-base//image",
    component = NAME,
    directory = "/",
    files = [":peribolos"],
    visibility = ["//visibility:public"],
)
